<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言之前的项目上有实现过文件的上传、下载的相关功能；这里大概想自己总结一下吧，下载的功能设计比较简单，因为文件内容体积不大，直接利用Blob对象进行下载操作实现即可。 而对于文件的上传操作，利用element的upload组件封装的API进行文件选择、类型判断、多文件上传、文件预览的解决。同样由于接口那边对文件类型有要求，还要加上判断和类型输入错误的提示；上传进度条也通过upload组件的方法实现">
<meta property="og:type" content="article">
<meta property="og:title" content="文件上传下载相关问题">
<meta property="og:url" content="http://example.com/2021/06/23/%E5%89%8D%E7%AB%AF/%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="前言之前的项目上有实现过文件的上传、下载的相关功能；这里大概想自己总结一下吧，下载的功能设计比较简单，因为文件内容体积不大，直接利用Blob对象进行下载操作实现即可。 而对于文件的上传操作，利用element的upload组件封装的API进行文件选择、类型判断、多文件上传、文件预览的解决。同样由于接口那边对文件类型有要求，还要加上判断和类型输入错误的提示；上传进度条也通过upload组件的方法实现">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-06-23T11:36:18.000Z">
<meta property="article:modified_time" content="2021-11-03T08:25:12.557Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="前端初学">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/06/23/%E5%89%8D%E7%AB%AF/%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>文件上传下载相关问题 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/06/23/%E5%89%8D%E7%AB%AF/%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          文件上传下载相关问题
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-06-23 19:36:18" itemprop="dateCreated datePublished" datetime="2021-06-23T19:36:18+08:00">2021-06-23</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:25:12" itemprop="dateModified" datetime="2021-11-03T16:25:12+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%89%8D%E7%AB%AF%E9%A1%B5%E9%9D%A2/" itemprop="url" rel="index"><span itemprop="name">前端页面</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>之前的项目上有实现过文件的上传、下载的相关功能；这里大概想自己总结一下吧，下载的功能设计比较简单，因为文件内容体积不大，直接利用Blob对象进行下载操作实现即可。</p>
<p>而对于文件的上传操作，利用element的upload组件封装的API进行文件选择、类型判断、多文件上传、文件预览的解决。同样由于接口那边对文件类型有要求，还要加上判断和类型输入错误的提示；上传进度条也通过upload组件的方法实现。但为了能够自定义上传效果，并没有直接用API提供的action,而是自己用axios.post()来进行异步上传。同时，由于上传的文件体积可能过大，因此需要考虑文件切片上传和断点续传的问题。</p>
<h1 id="文件下载"><a href="#文件下载" class="headerlink" title="文件下载"></a>文件下载</h1><h2 id="Blob对象"><a href="#Blob对象" class="headerlink" title="Blob对象"></a>Blob对象</h2><p>在 AJAX 请求中，指定 <code>responseType</code> 属性为 <code>blob</code> ，皆可以下下载一个 Blob 对象。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getBlob</span>(<span class="params">url, callback</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> xhr = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">  xhr.open(<span class="string">'GET'</span>, url);</span><br><span class="line">  xhr.responseType = <span class="string">'blob'</span>;</span><br><span class="line">  xhr.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    callback(xhr.response);</span><br><span class="line">  &#125;</span><br><span class="line">  xhr.send(<span class="literal">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="a标签实现下载"><a href="#a标签实现下载" class="headerlink" title="a标签实现下载"></a>a标签实现下载</h2><p>同时，之后我们可以使用URL.createObjectURL()方法，针对Blob对象生成一个临时URL；在生成一个a标签，将其href属性指定下载链接、dowload属性指定文件名，再利用click事件触发下载即可。下面是简单的示例代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">exportDam() &#123;</span><br><span class="line">    exportdam().then(<span class="function"><span class="keyword">function</span> (<span class="params">res</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!res) &#123;</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> url = <span class="built_in">window</span>.URL.createObjectURL(res.data);</span><br><span class="line">        <span class="keyword">let</span> alink = <span class="built_in">document</span>.createElement(<span class="string">'a'</span>);</span><br><span class="line">        alink.style.display = <span class="string">'none'</span>;</span><br><span class="line">        alink.href = url;</span><br><span class="line">        alink.setAttribute(<span class="string">'download'</span>, <span class="string">'xxxxx'</span> + <span class="string">'.xxxx'</span>);</span><br><span class="line">        <span class="built_in">document</span>.body.appendChild(alink);</span><br><span class="line">        alink.click();</span><br><span class="line">        <span class="built_in">document</span>.body.removeChild(alink);</span><br><span class="line">        <span class="built_in">window</span>.URL.revokeObjectURL(alink.href);</span><br><span class="line">    &#125;).catch(<span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(error);</span><br><span class="line">    &#125;)</span><br><span class="line"><span class="comment">// this.$http.get('http://xxxxxxxx/xxxxxx/xxxx',&#123;responseType:'blob'&#125;).then(res =&gt; &#123;</span></span><br><span class="line">          <span class="comment">//</span></span><br><span class="line">          <span class="comment">// &#125;)</span></span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h1 id="文件上传"><a href="#文件上传" class="headerlink" title="文件上传"></a>文件上传</h1><h2 id="功能实现"><a href="#功能实现" class="headerlink" title="功能实现"></a>功能实现</h2><p>在实现文件上传之前，我们先要判断文件的类型与后端所要求的文件类型是否一致，常见的方法如下：</p>
<h3 id="判断文件类型"><a href="#判断文件类型" class="headerlink" title="判断文件类型"></a>判断文件类型</h3><p>1、通过后缀名来判断文件的类型：一般情况下能够起到应有的效果，但是当用户人为地修改后缀名时，则这个方法就失去了作用，这个时候，我们就需要文件头的标准编码来进行判断文件的类别了。</p>
<p>2、文件头编码判断:一个文件的文件头会保存着该文件的类型的信息，因此我们封装一个方法，将文件头转化成16进制编码格式，之后要判断一个文件是什么类型时，直接将前4个字节传递进去。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> blobToString = <span class="function">(<span class="params">blob</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> reader = <span class="keyword">new</span> FileReader();</span><br><span class="line">    reader.onload = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> result = (reader.result)</span><br><span class="line">        .split(<span class="string">""</span>)</span><br><span class="line">				<span class="comment">// 转化成 asc 码</span></span><br><span class="line">        .map(<span class="function">(<span class="params">v</span>) =&gt;</span> v.charCodeAt(<span class="number">0</span>))</span><br><span class="line">				<span class="comment">// 转化成 16 进制</span></span><br><span class="line">        .map(<span class="function">(<span class="params">v</span>) =&gt;</span> v.toString(<span class="number">16</span>))</span><br><span class="line">				<span class="comment">// 补齐 0</span></span><br><span class="line">        .map(<span class="function">(<span class="params">v</span>) =&gt;</span> v.padStart(<span class="number">2</span>, <span class="string">"0"</span>))</span><br><span class="line">        .join(<span class="string">" "</span>);</span><br><span class="line">      resolve(result);</span><br><span class="line">    &#125;;</span><br><span class="line">    reader.readAsBinaryString(blob);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> isGif = <span class="keyword">async</span> (file) =&gt; &#123;</span><br><span class="line">  <span class="keyword">const</span> ret = <span class="keyword">await</span> blobToString(file.slice(<span class="number">0</span>, <span class="number">4</span>));</span><br><span class="line">  <span class="keyword">return</span> ret === <span class="string">"47 49 46 38"</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="FormData上传"><a href="#FormData上传" class="headerlink" title="FormData上传"></a>FormData上传</h3><p>在不考虑文件大小的情况下，文件上传的实现直接用FormData对象来组装键值对即可</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> files = e.target.files <span class="comment">// 获取input的file对象</span></span><br><span class="line"><span class="keyword">let</span> formData = <span class="keyword">new</span> FormData();</span><br><span class="line">formData.append(<span class="string">'file'</span>, file);</span><br><span class="line">axios.post(url, formData);</span><br></pre></td></tr></table></figure>

<h2 id="大文件上传"><a href="#大文件上传" class="headerlink" title="大文件上传"></a>大文件上传</h2><p>大文件上传最主要的问题就在于：<strong>在同一个请求中，要上传大量的数据，导致整个过程会比较漫长，且失败后需要重头开始上传</strong>。试想，如果我们将这个请求拆分成多个请求，每个请求的时间就会缩短，且如果某个请求失败，只需要重新发送这一次请求即可，无需从头开始，这样是否可以解决大文件上传的问题呢？</p>
<p>综合上面的问题，看来大文件上传需要实现下面几个需求</p>
<p>1、支持拆分上传请求(即切片)</p>
<p>2、支持断点续传</p>
<p>3、支持显示上传进度和暂停上传</p>
<p>接下来让我们依次实现这些功能，看起来最主要的功能应该就是切片了。</p>
<h3 id="文件切片上传"><a href="#文件切片上传" class="headerlink" title="文件切片上传"></a>文件切片上传</h3><p>在JavaScript中，文件FIle对象是Blob对象的子类，Blob对象包含一个重要的方法<code>slice</code>，通过这个方法，我们就可以对二进制文件进行拆分。这样将文件拆分成<code>piece</code>大小的分块，然后每次请求只需要上传这一个部分的分块即可</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  data: <span class="function"><span class="params">()</span> =&gt;</span> (&#123;</span><br><span class="line">    container: &#123;</span><br><span class="line">      file: <span class="literal">null</span></span><br><span class="line">    &#125;，</span><br><span class="line">   data: []</span><br><span class="line">  &#125;),</span><br><span class="line">  methods: &#123;</span><br><span class="line">    request() &#123;&#125;,</span><br><span class="line">    handleFileChange() &#123;&#125;,</span><br><span class="line">    <span class="comment">// 生成文件切片</span></span><br><span class="line">    createFileChunk(file, size = SIZE) &#123;</span><br><span class="line">     <span class="keyword">const</span> fileChunkList = [];</span><br><span class="line">      <span class="keyword">let</span> cur = <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">while</span> (cur &lt; file.size) &#123;</span><br><span class="line">        fileChunkList.push(&#123; <span class="attr">file</span>: file.slice(cur, cur + size) &#125;);</span><br><span class="line">        cur += size;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> fileChunkList;</span><br><span class="line">    &#125;,</span><br><span class="line">   <span class="comment">// 上传切片</span></span><br><span class="line">    <span class="keyword">async</span> uploadChunks() &#123;</span><br><span class="line">      <span class="keyword">const</span> requestList = <span class="keyword">this</span>.data</span><br><span class="line">       .map(<span class="function">(<span class="params">&#123; chunk，hash &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">         <span class="keyword">const</span> formData = <span class="keyword">new</span> FormData();</span><br><span class="line">          formData.append(<span class="string">"chunk"</span>, chunk);</span><br><span class="line">          formData.append(<span class="string">"hash"</span>, hash);</span><br><span class="line">          formData.append(<span class="string">"filename"</span>, <span class="keyword">this</span>.container.file.name);</span><br><span class="line">          <span class="keyword">return</span> &#123; formData &#125;;</span><br><span class="line">        &#125;)</span><br><span class="line">        .map(<span class="keyword">async</span> (&#123; formData &#125;) =&gt;</span><br><span class="line">          <span class="keyword">this</span>.request(&#123;</span><br><span class="line">            url: <span class="string">"http://localhost:3000"</span>,</span><br><span class="line">            data: formData</span><br><span class="line">          &#125;)</span><br><span class="line">        );</span><br><span class="line">      <span class="keyword">await</span> <span class="built_in">Promise</span>.all(requestList); <span class="comment">// 并发切片</span></span><br><span class="line">      <span class="keyword">await</span> <span class="keyword">this</span>.mergeRequest();</span><br><span class="line">    &#125;,</span><br><span class="line">     <span class="keyword">async</span> mergeRequest() &#123;</span><br><span class="line">      <span class="keyword">await</span> <span class="keyword">this</span>.request(&#123;</span><br><span class="line">        url: <span class="string">"http://localhost:3000/merge"</span>,</span><br><span class="line">        headers: &#123;</span><br><span class="line">          <span class="string">"content-type"</span>: <span class="string">"application/json"</span></span><br><span class="line">        &#125;,</span><br><span class="line">        data: <span class="built_in">JSON</span>.stringify(&#123;</span><br><span class="line">          filename: <span class="keyword">this</span>.container.file.name</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="keyword">async</span> handleUpload() &#123;</span><br><span class="line">      <span class="keyword">if</span> (!<span class="keyword">this</span>.container.file) <span class="keyword">return</span>;</span><br><span class="line">      <span class="keyword">const</span> fileChunkList = <span class="keyword">this</span>.createFileChunk(<span class="keyword">this</span>.container.file);</span><br><span class="line">      <span class="keyword">this</span>.data = fileChunkList.map(<span class="function">(<span class="params">&#123; file &#125;，index</span>) =&gt;</span> (&#123;</span><br><span class="line">        chunk: file,</span><br><span class="line">        hash: <span class="keyword">this</span>.container.file.name + <span class="string">"-"</span> + index <span class="comment">// 文件名 + 数组下标</span></span><br><span class="line">      &#125;));</span><br><span class="line">      <span class="keyword">await</span> <span class="keyword">this</span>.uploadChunks();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>整个的逻辑其实并不复杂：1、当点击上传按钮时，调用 <code>createFileChunk</code> 将文件切片，切片数量通过文件大小控制，这里设置 10MB，也就是说 100 MB 的文件会被分成 10 个切片。</p>
<p>2、createFileChunk 内使用 while 循环和 slice 方法将切片放入 <code>fileChunkList</code> 数组中返回</p>
<p>3、在生成文件切片时，需要给每个切片一个标识作为 hash，这里暂时使用<code>文件名 + 下标</code>，这样后端可以知道当前切片是第几个切片，用于之后的合并切片</p>
<p>4、随后调用 <code>uploadChunks</code> 上传所有的文件切片，将文件切片，切片 hash，以及文件名放入 FormData 中，再调用上一步的 <code>request</code> 函数返回一个 proimise，最后调用 Promise.all 并发上传所有的切片。</p>
<p>5、在上传完所有的切片后，需要再向服务器发送一个合并请求，即通知服务器切片已经上传完了，让服务器开始文件的合并的流程。</p>
<p>这样我们就实现了前端方面的文件切片传输的过程，并在最后传输完成之后，向服务端发送了一个文件合并的请求，告诉服务端文件已经传输完毕。</p>
<h3 id="文件拼接"><a href="#文件拼接" class="headerlink" title="文件拼接"></a>文件拼接</h3><p>用node的http模块构建服务器后，我们使用 <code>multiparty</code> 包处理前端传来的 FormData。在 multiparty.parse 的回调中，files 参数保存了 FormData 中文件，fields 参数保存了 FormData 中非文件的字段。</p>
<p>查看 multiparty 处理后的 chunk 对象，path 是存储临时文件的路径，size 是临时文件大小，在 multiparty 文档中提到可以使用 fs.rename移动临时文件，即移动文件切片。在接受文件切片时，需要先创建存储切片的文件夹，由于前端在发送每个切片时额外携带了唯一值 hash，所以以 hash 作为文件名，将切片从临时路径移动切片文件夹中。在接收到前端发送的合并请求后，服务端将文件夹下的所有切片进行合并</p>
<p>由于前端在发送合并请求时会携带文件名，服务端根据文件名可以找到上一步创建的切片文件夹。接着使用 fs.createWriteStream 创建一个可写流，可写流文件名就是<strong>切片文件夹名 + 后缀名</strong>组合而成。随后遍历整个切片文件夹，将切片通过 fs.createReadStream 创建可读流，传输合并到目标文件中。值得注意的是每次可读流都会传输到可写流的指定位置，这是通过 createWriteStream 的第二个参数 start/end 控制的，目的是能够并发合并多个可读流到可写流中，这样即使流的顺序不同也能传输到正确的位置，所以这里还需要让前端在请求的时候多提供一个 size 参数。</p>
<p>其实也可以等上一个切片合并完后再合并下个切片，这样就不需要指定位置，但传输速度会降低，所以使用了并发合并的手段，接着只要保证每次合并完成后删除这个切片，等所有切片都合并完毕后最后删除切片文件夹即可</p>
<h3 id="上传进度条"><a href="#上传进度条" class="headerlink" title="上传进度条"></a>上传进度条</h3><p>上传进度分两种，一个是每个切片的上传进度，另一个是整个文件的上传进度，而整个文件的上传进度是基于每个切片上传进度计算而来，所以我们先实现切片的上传进度</p>
<p>切片进度条：XMLHttpRequest 原生支持上传进度的监听，只需要监听 upload.onprogress 即可，我们在原来的 request 基础上传入 onProgress 参数，给 XMLHttpRequest 注册监听事件</p>
<p>由于每个切片都需要触发独立的监听事件，所以还需要一个工厂函数，根据传入的切片返回不同的监听函数。并在原先的前端上传逻辑中新增监听函数部分。每个切片在上传时都会通过监听函数更新 data 数组对应元素的 percentage 属性，之后把将 data 数组放到视图中展示即可</p>
<p>文件进度条：将每个切片已上传的部分累加，除以整个文件的大小，就能得出当前文件的上传进度，所以这里使用 Vue 计算属性。</p>
<h2 id="断点续传功能"><a href="#断点续传功能" class="headerlink" title="断点续传功能"></a>断点续传功能</h2><p>断点续传的原理在于前端/服务端需要<code>记住</code>已上传的切片，这样下次上传就可以跳过之前已上传的部分，有两种方案实现记忆的功能：</p>
<p>1、前端使用 localStorage 记录已上传的切片 hash；</p>
<p>2、服务端保存已上传的切片 hash，前端每次上传前向服务端获取已上传的切片；</p>
<p>第一种是前端的解决方案，第二种是服务端，而前端方案有一个缺陷，如果换了个浏览器就失去了记忆的效果，所以这里选取后者</p>
<h3 id="生成hash"><a href="#生成hash" class="headerlink" title="生成hash"></a>生成hash</h3><p>无论是前端还是服务端，都必须要生成文件和切片的 hash，<code>之前我们使用文件名 + 切片下标作为切片 hash</code>，这样做文件名一旦修改就失去了效果，而事实上只要文件内容不变，hash 就不应该变化，所以正确的做法是<code>根据文件内容生成 hash</code>，所以我们修改一下 hash 的生成规则</p>
<p>这里用到另一个库 <code>spark-md5</code>，它可以根据文件内容计算出文件的 hash 值，另外考虑到如果上传一个超大文件，读取文件内容计算 hash 是非常耗费时间的，并且会<code>引起 UI 的阻塞</code>，导致页面假死状态，所以我们使用 web-worker 在 worker 线程计算 hash，这样用户仍可以在主界面正常的交互</p>
<p>由于实例化 web-worker 时，参数是一个 js 文件路径且不能跨域，所以我们单独创建一个 hash.js 文件放在 public 目录下，另外在 worker 中也是不允许访问 dom 的，但它提供了<code>importScripts</code> 函数用于导入外部脚本，通过它导入 spark-md5。</p>
<p>在 worker 线程中，接受文件切片 fileChunkList，利用 FileReader 读取每个切片的 ArrayBuffer 并不断传入 spark-md5 中，每计算完一个切片通过 postMessage 向主线程发送一个进度事件，全部完成后将最终的 hash 发送给主线程。spark-md5 需要根据所有切片才能算出一个 hash 值，不能直接将整个文件放入计算，否则即使不同文件也会有相同的 hash，具体可以看官方文档</p>
<p>spark-md5 需要根据所有切片才能算出一个 hash 值，不能直接将整个文件放入计算，否则即使不同文件也会有相同的 hash，具体可以看官方文档</p>
<p>主线程使用 <code>postMessage</code> 给 worker 线程传入所有切片 fileChunkList，并监听 worker 线程发出的 postMessage 事件拿到文件 hash</p>
<p>服务端则使用 hash 作为切片文件夹名，hash + 下标作为切片名，hash + 扩展名作为文件名，没有新增的逻辑</p>
<h3 id="文件秒传"><a href="#文件秒传" class="headerlink" title="文件秒传"></a>文件秒传</h3><p>在实现断点续传前先简单介绍一下文件秒传：所谓的文件秒传，即在服务端已经存在了上传的资源，所以当用户<code>再次上传</code>时会直接提示上传成功。</p>
<p>文件秒传需要依赖上一步生成的 hash，即在<code>上传前</code>，先计算出文件 hash，并把 hash 发送给服务端进行验证，由于 hash 的唯一性，所以一旦服务端能找到 hash 相同的文件，则直接返回上传成功的信息即可。</p>
<h3 id="暂停上传"><a href="#暂停上传" class="headerlink" title="暂停上传"></a>暂停上传</h3><p>讲完了生成 hash 和文件秒传，回到断点续传。断点续传顾名思义即断点 + 续传，所以我们第一步先实现“断点”，也就是暂停上传。原理是使用 XMLHttpRequest 的 <code>abort</code> 方法，可以取消一个 xhr 请求的发送，为此我们需要将上传每个切片的 xhr 对象保存起来，我们再改造一下 request 方法。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">request(&#123;</span><br><span class="line">   url,</span><br><span class="line">   method = <span class="string">"post"</span>,</span><br><span class="line">   data,</span><br><span class="line">   headers = &#123;&#125;,</span><br><span class="line">   onProgress = <span class="function"><span class="params">e</span> =&gt;</span> e,</span><br><span class="line">   requestList</span><br><span class="line"> &#125;) &#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">const</span> xhr = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">     xhr.upload.onprogress = onProgress;</span><br><span class="line">     xhr.open(method, url);</span><br><span class="line">     <span class="built_in">Object</span>.keys(headers).forEach(<span class="function"><span class="params">key</span> =&gt;</span></span><br><span class="line">       xhr.setRequestHeader(key, headers[key])</span><br><span class="line">     );</span><br><span class="line">     xhr.send(data);</span><br><span class="line">     xhr.onload = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 将请求成功的 xhr 从列表中删除</span></span><br><span class="line">        <span class="keyword">if</span> (requestList) &#123;</span><br><span class="line">          <span class="keyword">const</span> xhrIndex = requestList.findIndex(<span class="function"><span class="params">item</span> =&gt;</span> item === xhr);</span><br><span class="line">          requestList.splice(xhrIndex, <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">       resolve(&#123;</span><br><span class="line">         data: e.target.response</span><br><span class="line">       &#125;);</span><br><span class="line">     &#125;;</span><br><span class="line">      <span class="comment">// 暴露当前 xhr 给外部</span></span><br><span class="line">      requestList?.push(xhr);</span><br><span class="line">   &#125;);</span><br><span class="line"> &#125;,</span><br></pre></td></tr></table></figure>

<p>这样在上传切片时传入 requestList 数组作为参数，request 方法就会将所有的 xhr 保存在数组中了。每当一个切片上传成功时，将对应的 xhr 从 requestList 中删除，所以 requestList 中只保存<code>正在上传切片的 xhr</code>。之后新建一个暂停按钮，当点击按钮时，调用保存在 requestList 中 xhr 的 abort 方法，即取消并清空所有正在上传的切片。点击暂停按钮可以看到 xhr 都被取消了。</p>
<h3 id="恢复上传"><a href="#恢复上传" class="headerlink" title="恢复上传"></a>恢复上传</h3><p>之前在介绍断点续传的时提到使用第二种服务端存储的方式实现续传</p>
<p>由于当文件切片上传后，服务端会建立一个文件夹存储所有上传的切片，所以每次前端上传前可以调用一个接口，服务端将已上传的切片的切片名返回，前端再跳过这些已经上传切片，这样就实现了“续传”的效果</p>
<p>而这个接口可以和之前秒传的验证接口合并，前端每次上传前发送一个验证的请求，返回两种结果</p>
<p>1、服务端已存在该文件，不需要再次上传</p>
<p>2、服务端不存在该文件或者已上传部分文件切片，通知前端进行上传，并把<strong>已上传</strong>的文件切片返回给前端。</p>
<p>所以我们改造一下之前文件秒传的服务端验证接口。接着回到前端，前端有两个地方需要调用验证的接口</p>
<p>1、点击上传时，检查是否需要上传和已上传的切片</p>
<p>2、点击暂停后的恢复上传，返回已上传的切片</p>
<p>新增恢复按钮并改造原来上传切片的逻辑；这里给原来上传切片的函数新增 uploadedList 参数，即上图中服务端返回的切片名列表，通过 filter 过滤掉已上传的切片，并且由于新增了已上传的部分，所以之前合并接口的触发条件做了一些改动。到这里断点续传的功能基本完成了。</p>
<h3 id="进度条修改"><a href="#进度条修改" class="headerlink" title="进度条修改"></a>进度条修改</h3><p>虽然实现了断点续传，但还需要修改一下进度条的显示规则，否则在暂停上传/接收到已上传切片时的进度条会出现偏差：</p>
<p>1、切片进度条：由于在点击上传/恢复上传时，会调用验证接口返回已上传的切片，所以需要将已上传切片的进度变成 100%。uploadedList 会返回已上传的切片，在遍历所有切片时判断当前切片是否在已上传列表里即可</p>
<p>2、文件进度条：</p>
<p>之前说到文件进度条是一个计算属性，根据所有切片的上传进度计算而来，这就遇到了一个问题：点击暂停会取消并清空切片的 xhr 请求，此时如果已经上传了一部分，就会发现文件进度条有<code>倒退</code>的现象。当点击恢复时，由于重新创建了 xhr 导致切片进度清零，所以总进度条就会倒退</p>
<p>解决方案是创建一个“假”的进度条，这个假进度条基于文件进度条，但只会停止和增加，然后给用户展示这个假的进度条。</p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>大文件上传</p>
<p>1、前端上传大文件时使用 Blob.prototype.slice 将文件切片，并发上传多个切片，最后发送一个合并的请求通知服务端合并切片</p>
<p>2、服务端接收切片并存储，收到合并请求后使用流将切片合并到最终文件</p>
<p>3、原生 XMLHttpRequest 的 upload.onprogress 对切片上传进度的监听</p>
<p>4、使用 Vue 计算属性根据每个切片的进度算出整个文件的上传进度</p>
<p>断点续传</p>
<p>1、使用 spark-md5 根据文件内容算出文件 hash</p>
<p>2、通过 hash 可以判断服务端是否已经上传该文件，从而直接提示用户上传成功（秒传）</p>
<p>3、通过 XMLHttpRequest 的 abort 方法暂停切片的上传</p>
<p>4、上传前服务端返回已经上传的切片名，前端跳过这些切片的上传</p>

    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/06/23/%E5%89%8D%E7%AB%AF/%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/" title="文件上传下载相关问题">http://example.com/2021/06/23/前端/文件上传下载相关问题/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%89%8D%E7%AB%AF%E5%88%9D%E5%AD%A6/" rel="tag"># 前端初学</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/06/10/%E5%AD%A6%E6%A0%A1%E9%A1%B9%E7%9B%AE%E5%BA%94%E7%94%A8/leaflet.js/" rel="prev" title="leaflet.js">
      <i class="fa fa-chevron-left"></i> leaflet.js
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/07/06/%E9%9D%A2%E8%AF%95%E6%80%BB%E7%BB%93/%E4%B8%80%E5%8F%A5%E8%AF%9D%E5%9B%9E%E7%AD%94JS%E9%97%AE%E9%A2%98/" rel="next" title="一句化回答JS问题">
      一句化回答JS问题 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件下载"><span class="nav-number">2.</span> <span class="nav-text">文件下载</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Blob对象"><span class="nav-number">2.1.</span> <span class="nav-text">Blob对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#a标签实现下载"><span class="nav-number">2.2.</span> <span class="nav-text">a标签实现下载</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文件上传"><span class="nav-number">3.</span> <span class="nav-text">文件上传</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#功能实现"><span class="nav-number">3.1.</span> <span class="nav-text">功能实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#判断文件类型"><span class="nav-number">3.1.1.</span> <span class="nav-text">判断文件类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FormData上传"><span class="nav-number">3.1.2.</span> <span class="nav-text">FormData上传</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#大文件上传"><span class="nav-number">3.2.</span> <span class="nav-text">大文件上传</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#文件切片上传"><span class="nav-number">3.2.1.</span> <span class="nav-text">文件切片上传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件拼接"><span class="nav-number">3.2.2.</span> <span class="nav-text">文件拼接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#上传进度条"><span class="nav-number">3.2.3.</span> <span class="nav-text">上传进度条</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#断点续传功能"><span class="nav-number">3.3.</span> <span class="nav-text">断点续传功能</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#生成hash"><span class="nav-number">3.3.1.</span> <span class="nav-text">生成hash</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件秒传"><span class="nav-number">3.3.2.</span> <span class="nav-text">文件秒传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#暂停上传"><span class="nav-number">3.3.3.</span> <span class="nav-text">暂停上传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#恢复上传"><span class="nav-number">3.3.4.</span> <span class="nav-text">恢复上传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#进度条修改"><span class="nav-number">3.3.5.</span> <span class="nav-text">进度条修改</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

